/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classes;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

/**
 * This class sets up a mail and it sends it to its receiver; It is also
 * possible to create a link with the method setMessage(location, keys).
 *
 * @author Sander
 */
public class Mail {

    private final static String username = "3twolearn.pdlteam9@gmail.com";
    private final static String password = "pdlteam9";
    private final static String host = "smtp.gmail.com";
    private final static String from = "3twolearn.pdlteam9@gmail.com";
    private final static String test = "sander.tetteroo.1992@gmail.com";

    /* //Remove the '/*' tags for sending a test mail
     public static void main(String[] args) {
     try {
     sendMail(test, "test", "test");
     } catch (Exception ex) { }
     }
     */
    public Mail() {
    }

    /**
     * This method generates a link with the given location and keys.
     *
     * @param location
     * @param keys
     * @return
     * @throws MalformedURLException
     * @throws IOException
     */
    public static String setMessage(String location, String keys[][]) throws MalformedURLException, IOException {
        String message = "";
        String s = "";
        String output = "";

        // Here a link will be generated with the given keys
        if (keys[0][0] != null) {
            StringBuilder sb = new StringBuilder();
            sb.append("?").append(keys[0][0]).append("=").append(keys[0][1]);
            for (int i = 1; i < keys.length; i++) {
                sb.append("&").append(keys[i][0]).append("=").append(keys[i][1]);
            }
            output = sb.toString();
        }

        // Creates a new url
        URL url = new URL(location + output);
        URLConnection connection = url.openConnection();

        DataInputStream dis = new DataInputStream(connection.getInputStream());
        InputStreamReader isr = new InputStreamReader(dis);
        BufferedReader br = new BufferedReader(isr);

        while ((s = br.readLine()) != null) {
            message += s + "\n";
        }
        return message;
    }

    /**
     * This method sends the mail to the receiver; It will go through some
     * settings and if they are correctly it will send it through SMTP.
     *
     * @param to
     * @param fullname
     * @param subject
     * @param message
     * @throws Exception
     */
    public static void sendMail(String to, String fullname, String subject, String message) throws Exception {
        Properties properties = System.getProperties(); // Get system properties

        // Setup mail server
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", "587");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.ehlo", "true");
        properties.put("mail.smtp.auth", "true");

        // Get the default Session object.
        Session session = Session.getInstance(properties,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
        /* //Remove the '/*' tags for enable debug mode
        session.setDebug(true);
        */

        MimeMessage mmessage = new MimeMessage(session); // Default MimeMessage object
        mmessage.setFrom(new InternetAddress(from, "3TwoLearn")); // From

        mmessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to, fullname)); // To
        mmessage.setSubject(subject); // Subject
        mmessage.setContent(message, "text/html"); // Message

        // Send message
        Transport transport = session.getTransport("smtp");
        transport.connect();
        transport.sendMessage(mmessage, mmessage.getAllRecipients());
        transport.close();
    }
}
